<?php

namespace app\admin\controller;

use think\facade\Session;
use app\admin\model\Admin as AdminModel;
use app\admin\model\Rule as RuleModel;

class Base extends \app\BaseController
{
    /**
     * 登录白名单
     */
    private $no_login = [
        'Login'         =>  [
            'index', 'out'
        ]
    ];

    /**
     * 权限白名单
     */
    private $no_auth = [
        'Index'         =>  [
            'index'
        ]
    ];

    /**
     * 模型名称
     */
    public $model;

    /**
     * 验证器名称
     */
    public $validate = '';

    public function initialize()
    {
        parent::initialize();
        if ($this->check_login() === false) {
            return;
        }

        if (!Session::has('admin')) {
            return $this->error('请先登录', 'admin/login/index');
        }

        $where = [
            ['id', '=', Session::get('admin.id')],
        ];
        $admin = AdminModel::with(['group'])->field('id,username,token,status,group_id,avatar')->where($where)->find();
        
        if ($admin['token'] != Session::get('admin.token')) {
            return $this->error('当前账户已再其他地方登陆,请重新登陆', 'admin/login/index');
        }
        $admin->hidden(['group_id']);
        $this->admin = $admin;
        $this->assign('admin', $admin);
        $this->menu_info();
        
        if (!$this->model) {
            $this->model = $this->controller;
        }
        if (!$this->validate) {
            $this->validate = $this->model;
        }
    }


    /**
     * 检测是否登录
     */
    private function check_login()
    {
        $no_login = $this->no_login;
        if (empty($no_login[$this->controller]) || !in_array($this->action, $no_login[$this->controller])) {
            return true;
        }
        return false;
    }

    /**
     * 检测权限
     */
    private function check_auth()
    {
        $no_auth = $this->no_auth;
        if ((empty($no_auth[$this->controller]) || !in_array($this->action, $no_auth[$this->controller])) && ($this->admin['group']['rules'] != '*' && !in_array($this->menu_info['id'], explode(',', $this->admin['group']['rules'])))) {
            if($this->controller=='Index'&&$this->action=='edit'&&$this->get['id']==$this->admin['id']){
                return;
            }
            return $this->error('暂无权限,请联系管理员');
        }
    }

    /**
     * 获取当前模块
     */
    private function menu_info()
    {
        $where = [
            'url' =>  'admin/' . strtolower($this->controller) . '/' . $this->action,
        ];
        $this->menu_info = RuleModel::where($where)->find();
        if ($this->menu_info) {
            $this->check_auth();
            $this->menu_list();
            $this->assign('menu_info', $this->menu_info);
        } else {
            return $this->error('模块不存在');
        }
    }

    /**
     * 左侧菜单
     */
    private function menu_list()
    {
        if ($this->request->isAjax() || $this->request->isPost()) {
            return;
        }
        $where = [
            ['is_show', '=', 1],
            ['status', '=', 1],
        ];
        if ($this->admin['group']['rules'] != '*') {
            $where[] = ['id', 'in', $this->admin['group']['rules']];
        }

        $list = RuleModel::where($where)->cache('admin_menu_list')->order('sort desc')->select();
        $list->append(['optionss']);
        // dump($list->toArray());exit;
        $menu_list = children_menu($this->menu_info, $list->toArray());

        // dump($menu_list);exit;
        $this->assign('menu_list', $menu_list);
        //获取当前模块所有父级模块
        $menu_path = menu_path($list, $this->menu_info['pid']);
        $this->assign('menu_path', array_reverse($menu_path));
    }
}
